import { withMiddleware } from '@/lib/middleware/entry';
import { NextRequest, NextResponse } from 'next/server';
import { authenticateRequest } from '@/utils/auth';

// 强制动态渲染
export const dynamic = 'force-dynamic';

// POST /api/auth/me - 获取当前登录的用户信息
async function handlePost(request: NextRequest) {
  try {
    const authResult = await authenticateRequest(request);
    
    if (!authResult.success || !authResult.user) {
      return NextResponse.json(
        {
          success: false,
          message: authResult.error || '认证失败',
        },
        { status: 401 }
      );
    }

    const { password, ...safeUser } = authResult.user;

    return NextResponse.json({
      success: true,
      data: safeUser
    });
  } catch (error) {
    console.error('获取用户信息失败:', error);
    return NextResponse.json(
      {
        success: false,
        message: '获取用户信息失败',
        error: error instanceof Error ? error.message : '未知错误',
      },
      { status: 500 }
    );
  }
}

export const POST = withMiddleware(handlePost);
